package cse3311.calendarapp;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class StorageManager {
	public static final String fileName = "calendar.ser";

	public static String saveCalendar(File dir) {
		String filePath = dir + "/" + fileName;
		Calendar calendar = Calendar.getCalendar();

		try {
			File file = new File(filePath);
			FileOutputStream fileStream = new FileOutputStream(file);
			ObjectOutputStream objectOut = new ObjectOutputStream(fileStream);
			objectOut.writeObject(calendar);
			objectOut.flush();
			objectOut.close();

			// also write to a byte array to save to Parse
			ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
			ObjectOutputStream byteObjectOut = new ObjectOutputStream(
					byteStream);
			byteObjectOut.writeObject(calendar);
			byteObjectOut.flush();
			byteObjectOut.close();

			//ParseFile parseFile = new ParseFile(byteStream.toByteArray());
			//parseFile.save();

		} catch (Exception e) {
			return "Calendar not saved";
		}

		return "Calendar saved";
	}

	public static void loadCalendar(File dir) {
		String filePath = dir + "/" + fileName;
		Calendar calendar = null;
		try {
			FileInputStream file = new FileInputStream(filePath);
			ObjectInputStream objectIn = new ObjectInputStream(file);
			calendar = (Calendar) objectIn.readObject();
			objectIn.close();
		} catch (Exception e) {
			Calendar.initialize();
			return;
		}

		Calendar.setInstance(calendar);
	}

	public static void delete(File dir) {
		File file = new File(dir + "/" + fileName);
		file.delete();
	}

	public static void setupGroup(String groupName) {
		ParseObject o = new ParseObject("GroupTimeSlot");
		o.put("groupName", groupName);

		JSONArray year = new JSONArray();
		for (int i = 0; i < 365; i++) {
			JSONArray day = new JSONArray();
			for (int j = 0; j < 24; j++) {
				try {
					day.put(j, 0);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			try {
				year.put(i, day);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		o.put("timeSlots", year);

		try {
			o.save();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void updateGroupTimeSlots(String groupName, int day,
			int hour, boolean addEvent) {
		ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
				"GroupTimeSlots");

		query.whereEqualTo("groupName", groupName);

		try {
			List<ParseObject> result = query.find();

			ParseObject object = result.get(0);
			int count = object.getJSONArray("timeSlots").getJSONArray(day)
					.getInt(hour);

			if (addEvent) {
				count++;
			} else {
				if (count > 0) {
					count--;
				}
			}

			object.getJSONArray("timeSlots").getJSONArray(day).put(hour, count);
		} catch (Exception e) {
			return;
		}
	}
}
